package io.renren.modules.store.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.common.utils.BeanJudgeUtil;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.store.entity.TStoreReasonDetailEntity;
import io.renren.modules.store.service.TStoreReasonDetailService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-25 02:05:20
 */
@RestController
@RequestMapping("store/tstorereasondetail")
public class TStoreReasonDetailController extends AbstractController {
    @Autowired
    private TStoreReasonDetailService tStoreReasonDetailService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstorereasondetail:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        params.put("key",params.get("key"));
        PageUtils page = tStoreReasonDetailService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 赠菜理由
     */
    @RequestMapping("/listGiveFood")
    @RequiresPermissions("store:tstorereasondetail:list")
    public R listGiveFood(){
        List<TStoreReasonDetailEntity> reasonList = tStoreReasonDetailService.list(
                new QueryWrapper<TStoreReasonDetailEntity>()
                        .eq("reason_id", 3)
        );
        return R.ok().put("data",reasonList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{reasonDetailId}")
    @RequiresPermissions("store:tstorereasondetail:info")
    public R info(@PathVariable("reasonDetailId") Long reasonDetailId){
		TStoreReasonDetailEntity tStoreReasonDetail = tStoreReasonDetailService.getById(reasonDetailId);

        return R.ok().put("tStoreReasonDetail", tStoreReasonDetail);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("store:tstorereasondetail:save")
    public R save(@RequestBody TStoreReasonDetailEntity tStoreReasonDetail){
        TStoreReasonDetailEntity one = tStoreReasonDetailService.getOne(new QueryWrapper<TStoreReasonDetailEntity>()
                .eq("seller_id", getSeller())
                .eq("reason_id", tStoreReasonDetail.getReasonId())
                .eq("reason_detail_name", tStoreReasonDetail.getReasonDetailName())
        );
        if (!BeanJudgeUtil.beanIsNull(one)){
            throw new RRException("理由已存在,不能重复添加");
        }
        tStoreReasonDetail.setSellerId(getSeller());
		tStoreReasonDetailService.save(tStoreReasonDetail);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstorereasondetail:update")
    public R update(@RequestBody TStoreReasonDetailEntity tStoreReasonDetail){
		tStoreReasonDetailService.updateById(tStoreReasonDetail);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstorereasondetail:delete")
    public R delete(@RequestBody Long[] reasonDetailIds){
		tStoreReasonDetailService.removeByIds(Arrays.asList(reasonDetailIds));

        return R.ok();
    }

}
